package com.lagou.service.email.controller;

import com.lagou.service.email.service.EmailService;
import com.lagou.service.email.service.feign.UserServiceFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private EmailService emailService;

    @Autowired
    private UserServiceFeignClient userServiceFeignClient;

    /**
     * 发送验证码到邮箱，true成功，false失败
     */
    @PostMapping("/{email}/{code}")
    public Boolean send(@PathVariable @NotBlank(message = "邮箱不能为空") String email,
                        @PathVariable @NotBlank(message = "验证码不能为空") String code) {

        /** 1 检查该邮箱是否已经注册过 */
        Boolean isRegistered = userServiceFeignClient.isRegistered(email);
        if (isRegistered) {
            return false;
        }

        /** 2 邮箱没有注册的场合，发送验证码到该邮箱 */
        return emailService.send(email, code);
    }
}
